﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emLanguage : emMasterDetailEdit
    {
        private data.baseDS.emLangDataTable myDataTbl = new data.baseDS.emLangDataTable();
        private data.baseDS.languageDataTable myLanguageTbl = new data.baseDS.languageDataTable();
        public emLanguage()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);;
            }            
        }

        public override event EventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                emLanguageDetail.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
            emLanguageDetail.myOnError -= value;
            }
        }

        public override void Init()
        {
            base.Init();
            emLanguageDetail.Init();
            SetData(myDataTbl);

            myLanguageTbl.Clear();
            application.dataLibs.LoadData(myLanguageTbl);
            languageSource.DataSource = myLanguageTbl;
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                application.dataLibs.LoadData(myDataTbl, emCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = emCode;
            }
            application.dataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state);
            emLanguageDetail.LockEdit(state);
        }
        public override bool CheckData()
        {
            return emLanguageDetail.CheckData();
        }
        public override void SetFocus()
        {
            emLanguageDetail.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emLangRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emLangRow)((DataRowView)this.gridDataSource.Current).Row;
                application.dataLibs.InitData(row);
            }
            emLanguageDetail.SetDataSource(gridDataSource);
        }

        private void SetData(data.baseDS.emLangDataTable tbl)
        {
            gridDataSource.DataSource = tbl;
            emLanguageDetail.SetDataSource(gridDataSource);
            DataGridViewComboBoxColumn languageColumn = new DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn langLevel = new DataGridViewTextBoxColumn();
            this.detailGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] 
                                                {languageColumn, langLevel}
                                         );

            // 
            // languageColumn
            // 
            languageColumn.DataPropertyName = tbl.langCodeColumn.ColumnName;
            languageColumn.DataSource = languageSource;
            languageColumn.DisplayMember = myLanguageTbl.descriptionColumn.ColumnName;
            languageColumn.ValueMember = myLanguageTbl.codeColumn.ColumnName;
            languageColumn.HeaderText = "Ngọai ngữ";
            languageColumn.Name = "languageName";
            languageColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            languageColumn.ReadOnly = true;

            // 
            // langLevel
            // 
            langLevel.DataPropertyName = tbl.levelColumn.ColumnName;
            langLevel.HeaderText = "Cấp.độ";
            langLevel.Width = 120;
            langLevel.Name = tbl.levelColumn.ColumnName;
            langLevel.ReadOnly = true;

            common.system.AutoFitGridColumn(detailGrid, languageColumn.Name);
        }
    }
}
